package com.polaris.live.common.cacha

import com.polaris.live.common.resp.AppLandingPageVo
import com.polaris.live.common.resp.AppPrimaryPageVo
import com.polaris.live.common.resp.AppSecondaryPageVo

/**
 * @autuor: 天晴
 * @data: 2023/12/20 17:45
 * @description: 显示控制
 **/
object DisplayUtil {

    /**
     * 一级页面ID
     */
    const val LIVE = 3              //直播页
    const val HOT = 2               //热门页
    const val GAME = 6              //游戏页
    const val MESSAGE = 4           //私聊页
    const val MINE = 5              //我的页
    const val COMMUNITY = 7         //社区页

    /**
     * 二级页面ID
     */
    const val HOT_HOT = 21              //热门
    const val HOT_NEWCOMER = 22         //新人
    const val HOT_RECOMMEND = 23        //热门推荐
    const val LIVE_LIVE = 31            //直播
    const val LIVE_PARTY = 32           //派对 sala is 交友
    const val LIVE_FOLLOW = 33          //关注
    const val LIVE_MINE = 34            //交友页 sala is 我的
    const val MESSAGE_MESSAGE = 41      //消息
    const val MESSAGE_FAMILY = 42       //家族
    const val MINE_MINE = 51            //我的
    const val GAME_MATCH = 61           //游戏匹配页面
    const val COMMUNITY_COMMUNITY = 71  //社区之社区页
    const val COMMUNITY_RECOMMEND = 73  //社区推荐
    const val COMMUNITY_NEWCOMER = 74   //社区新人
    const val COMMUNITY_STORE = 75       //社区商城

    private var defaultAppSecondaryPageVo: AppSecondaryPageVo =
        AppSecondaryPageVo(LIVE, LIVE_LIVE)  //默认用户指定跳转的界面
    private val defaultDisplayPage = listOf( //默认需要展示的界面
        AppPrimaryPageVo(LIVE, listOf(LIVE_LIVE, LIVE_PARTY, LIVE_MINE,LIVE_FOLLOW), LIVE_PARTY),
        AppPrimaryPageVo(GAME, listOf(GAME_MATCH)),
        AppPrimaryPageVo(
            COMMUNITY,
            listOf(COMMUNITY_RECOMMEND, COMMUNITY_NEWCOMER, COMMUNITY_COMMUNITY, COMMUNITY_STORE)
        ),
        AppPrimaryPageVo(MESSAGE, listOf(MESSAGE_MESSAGE)),
        AppPrimaryPageVo(MINE, listOf(MINE_MINE))
    ).toMutableList()

    private var cacheAppLandingPageVo: AppLandingPageVo? = null

    /**
     * 存首页页面
     */
    fun setAppLandingPage(landingPage: AppLandingPageVo) {
        cacheAppLandingPageVo = landingPage

        CacheUtil.setAppLandingPage(landingPage)
    }

    /**
     * 获取页面数据
     */
    fun getAppLandingPage(): AppLandingPageVo? {
        return cacheAppLandingPageVo ?: let {
            CacheUtil.getAppLandingPage()?.apply {
                cacheAppLandingPageVo = this
            }
        }
    }

    /**
     * 获取一级页面page
     */
    fun getDisplayPage(): MutableList<AppPrimaryPageVo> {
        return getAppLandingPage()?.displayPage ?: defaultDisplayPage
    }

    /**
     * 获取二级页面page
     */
    fun getAppSecondaryPageVo(): AppSecondaryPageVo {
        return getAppLandingPage()?.landingPage ?: defaultAppSecondaryPageVo
    }

    /**
     * 根据当前CurrentIndex获取PrimaryId
     */
    fun getCurrentIndexByPrimaryId(primaryPage: Int): Int {
        val index = getDisplayPage().indexOfFirst { it.primaryPage == primaryPage }
        return if (index != -1) index else 0
    }

    /**
     * 根据当前PrimaryId获取CurrentIndex
     */
    fun getPrimaryIdByCurrentIndex(currentIndex: Int): Int {
        return getDisplayPage().getOrNull(currentIndex)?.primaryPage ?: 1
    }

    /**
     * 获取消息页Id
     */
    fun getMessagePageId(): Int {
        return getDisplayPage().indexOfFirst { it.primaryPage == MESSAGE }
    }

    /**
     * 获取我的页面Id
     */
    fun getMePageId(): Int {
        return getDisplayPage().indexOfFirst { it.primaryPage == MINE }
    }

    /**
     * 获取首页的页面Id
     */
    fun getHomePageId(): Int {
        return getDisplayPage().indexOfFirst { it.primaryPage == LIVE }
    }

    /**
     * 获取根据一级id二级页面列表
     */
    fun getSecondaryPagesByPrimaryPage(primaryPage: Int): AppPrimaryPageVo? {
        return getDisplayPage().firstOrNull { it.primaryPage == primaryPage }
    }

    /**
     * 根据当前二级的PrimaryId获取CurrentIndex
     */
    fun getCurrentIndexBySecondaryPrimaryId(primaryPage: Int, secondaryPage: Int): Int {
        val secondary =
            getDisplayPage().firstOrNull { it.primaryPage == primaryPage }?.secondaryPages
        val index = secondary?.indexOfFirst { it == secondaryPage } ?: 0
        return if (index != -1) index else 0
    }

    /**
     * 获取默认应该显示的二级页面
     */
    fun getDefaultIndexBySecondaryPrimaryId(gotoIndex: Int?, primaryPage: Int, page: AppPrimaryPageVo?): Int? {
        val index = gotoIndex ?: page?.secondaryMainPage?.let {
            getCurrentIndexBySecondaryPrimaryId(primaryPage, it)
        } ?: 0
        return if (index < (page?.secondaryPages?.size ?: 0)) {
            index
        } else {
            null
        }
    }
}